package cc.plumgo.ss.ui.page;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;

import cc.plumgo.j2me.utils.StringUtils;
import cc.plumgo.ss.core.Console;
import cc.plumgo.ss.core.Constants;
import cc.plumgo.ss.ui.UIEngine;

public class StartPage extends ShowPage {
	public static final int SRT_NEW = 0;
	public static final int SRT_GOON = 1;
	public static final int SRT_RET = 2;
	public static final int SRT_STATE_NUM = 3;
	
	private int selected;
	private Sprite flwSprite;
	private String flwName;
	private int frmWidth;
	private int frmHeight;
	private boolean isReturn = false;
	private Font font;
	private int color = 0xFFFFFFFF;

	public StartPage(String imgName, int x, int y) {
		super(imgName, x, y);
		selected = SRT_NEW;
	}

	public boolean isReturn() {
		return isReturn;
	}

	public void setSprite(String flw, int width, int height) {
		flwName = flw;
		frmWidth = width;
		frmHeight = height;
	}

	public void process(int keyStates, Graphics g) {
		drawBackImage(g);
		loadSprite(Constants.UI_IMG_ARROW, Constants.UI_ARROW_FRM_SIZ[0],
				Constants.UI_ARROW_FRM_SIZ[1]);
		switch (state) {
		case SHOW_HIDE:
			break;
		case SHOW_SHOW: {
			loadSprite();
			if ((keyStates & GameCanvas.UP_PRESSED) != 0) {
				selected = (-- selected < 0) ? (SRT_STATE_NUM - 1) : selected;
				
			}
			if ((keyStates & GameCanvas.DOWN_PRESSED) != 0) {
				selected = (++ selected >= SRT_STATE_NUM) ? SRT_NEW : selected;
			}
			
			if ((keyStates & GameCanvas.FIRE_PRESSED) != 0) {
				switch(selected) {
				case SRT_NEW : {
					Console.getInstance().getGameListener().showPage(UIEngine.UI_HEAD);
					break;
				}
				case SRT_GOON : {
					Console.getInstance().getGameListener().resume();
					break;
				}
				case SRT_RET : {
					state = SHOW_HIDE;
					isReturn = true;
					break;
				}
				}
				
			}
			
			flwSprite.setVisible(false);
			if (SRT_RET != selected) {
				int x = 52;
				int y = (SRT_NEW == selected) ? 55 : 140;
				flwSprite.setVisible(true);
				flwSprite.setPosition(x, y);
				flwSprite.paint(g);
			}
			
			loadFont();
			g.setFont(font);
			int back = g.getColor();
			g.setColor(0x00000000);
			drawStartText(g);
			drawGoOnText(g);
			
			g.setColor(color);
			switch(selected) {
			case SRT_NEW : {
				drawStartText(g);
				break;
			}
			case SRT_GOON : {
				drawGoOnText(g);
				break;
			}
			case SRT_RET : {
				drawArrow(g);
				break;
			}
			}
			g.setColor(back);
			delay = (-- delay < 0)? 5 : delay;
			break;
		}
		}
	}

	private void loadFont() {
		if (font == null) {
			try {
				font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);
			} catch(Exception e) {
				font = Font.getDefaultFont();
			}
		}
	}
	
	private void loadSprite() {
		if (flwSprite == null) {
			try {
				Image img = Image.createImage(flwName);
				flwSprite = new Sprite(img, frmWidth, frmHeight);
			} catch (Exception e) {
				e.printStackTrace();
			}
			flwSprite.defineReferencePixel(frmWidth / 2, frmHeight / 2);
			flwSprite.setFrame(0);
		}
	}
	
	public void drawStartText(Graphics g) {
		StringUtils.adjustDrawString(g, Constants.UI_TXT_SRT_NEW, 100, 120, 70, 10);
	}
	
	public void drawGoOnText(Graphics g) {
		StringUtils.adjustDrawString(g, Constants.UI_TXT_SRT_GOON, 100, 120, 155, 10);
	}
}
